package com.example.myapplication.newuser;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

/**
 * @Description: 客户每隔10分钟获得唱片
 * @Author: rongfeng
 * @CreateDate: 2019/1/22 14:51
 */
public class ReceiveFreeGiftModel {

    private ReceiveFreeGiftModel() {
    }

    private static volatile ReceiveFreeGiftModel instance = null;

    public static ReceiveFreeGiftModel getInstance() {
        if (instance == null) {
            synchronized (ReceiveFreeGiftModel.class) {
                if (instance == null) {
                    instance = new ReceiveFreeGiftModel();
                }
            }
        }
        return instance;
    }

    private long startTime;
    private long needTime = 1000 * 60 * 5;
    private int loopTime = 60000;


//    public static long needTime = 60000;
//    private int  loopTime = 3000;


    /**
     * 开始计时
     */
    public void startTime() {
        startTime = System.currentTimeMillis();
        mHandler.sendEmptyMessageDelayed(1, loopTime);
    }

    /**
     * 结束计时
     */
    private void loopTime() {
        long currentTime = System.currentTimeMillis();
        if (startTime != 0 && currentTime - startTime > needTime) {
            startTime = currentTime;
            getLiveAdvertising();
        }
    }


    /**
     * 获取停留时间 s
     *
     * @return
     */
    public long getStayTime() {
        return System.currentTimeMillis() - startTime;
    }

    /**
     * 退出直播间，清零
     */
    public void clear() {
        startTime = 0;
        mHandler.removeMessages(1);
    }

    private void getLiveAdvertising() {

    }

    public void onDestroy() {
        startTime = 0;
    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                loopTime();
                mHandler.sendEmptyMessageDelayed(1, loopTime);
            }
        }
    };
}
